package com.roads.web.controller.system;

import java.util.List;

import com.roads.common.constant.UserConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.roads.common.annotation.Log;
import com.roads.common.core.controller.BaseController;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.enums.BusinessType;
import com.roads.system.domain.SysPaymentItems;
import com.roads.system.service.ISysPaymentItemsService;
import com.roads.common.utils.poi.ExcelUtils;
import com.roads.common.core.page.TableDataInfo;

/**
 * 支付项目Controller
 * 
 * @author nhp
 * @date 2022-03-11
 */
@RestController
@RequestMapping("/system/payment")
public class SysPaymentItemsController extends BaseController {

    @Autowired
    private ISysPaymentItemsService service;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:payment:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPaymentItems model) {
        startPage();
        List<SysPaymentItems> list = service.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 支付项目下拉选项
     *
     * @param itemCode 项目编号
     * @return 结果
     */
    @GetMapping("/selectOptionsList")
    public AjaxResult selectOptionsList(String itemCode) {
        List<SysPaymentItems> list = service.selectOptionsList(itemCode);
        return AjaxResult.success(list);
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:payment:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Integer itemId) {
        return AjaxResult.success(service.getModelById(itemId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:payment:add')")
    @Log(title = "支付项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPaymentItems model) {
        if (UserConstants.NOT_UNIQUE.equals(service.checkItemNameUnique(model))) {
            return AjaxResult.error("新增支付项目'" + model.getItemName() + "'失败，项目名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(service.checkItemCodeUnique(model))) {
            return AjaxResult.error("新增支付项目'" + model.getItemCode() + "'失败，项目编号已存在");
        }
        model.setCreateBy(getUsername());
        return toAjax(service.insert(model));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:payment:edit')")
    @Log(title = "支付项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPaymentItems model) {
        if (UserConstants.NOT_UNIQUE.equals(service.checkItemNameUnique(model))) {
            return AjaxResult.error("修改支付项目'" + model.getItemName() + "'失败，项目名称已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(service.checkItemCodeUnique(model))) {
            return AjaxResult.error("修改支付项目'" + model.getItemCode() + "'失败，项目编号已存在");
        }
        return toAjax(service.update(model));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:payment:remove')")
    @Log(title = "支付项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(service.deleteByIds(ids));
    }
}
